#include "fs.h"
#include "zip.h"
#include "kramdisk.h"
#include "initrd_array.h"
#include "dbgio.h"
#include "ifs.h"

void initialize_initrd(){
	vfs_node_t *rootdir;
	vfs_node_t *romdir;
	vfs_node_t *zipfile;
	vfs_node_t *mp;
	ifs_descriptor_t *ird_romfs_desc = ifs_get_desc("romfs");
	dprintf("Creating initrd ramdisk...");
	rootdir = kramdisk_mkfs("initrd");
	dprintf("OK\nLoading initrd romdisk...");
	romdir = ird_romfs_desc->ifs_mount_cb((vfs_node_t *)initrd_rom);
	dprintf("OK\nExtracting /bin/rootfs.zip ...");
	zipfile = finddir_fs(romdir,"rootfs.zip");
	dprintf("rootfs.zip 0x%x",zipfile->impl);
	krnl_zip_extract_to(rootdir,(void *)zipfile->impl);//Direct pointer to rootfs.zip data segment
	dprintf("OK\nMounting initrd romdisk on /sbin...");
	mp = finddir_fs(rootdir,"sbin");
	mp->ptr = romdir;
	mp->flags |= VFS_MOUNTPOINT_FLAG;
	dprintf("OK\nChrooting to initrd ramdisk");
	chroot_fs(rootdir);
	dprintf("OK\n");
}